package com.gxy.learn.design.model.construction.proxy.cglb_proxy;

import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @description:
 * @author: gaoxueyong
 * @Date: 2022/11/7 17:51
 */
@Slf4j
public class ProxyFactory implements MethodInterceptor {
    public TrainStation trainStation = new TrainStation();
    public TrainStation getProxyObject (){
//        创建Enhancer对象
        Enhancer enhancer = new Enhancer();
//        设置父类的字节码
        enhancer.setSuperclass(TrainStation.class);
//        设置回调函数
        enhancer.setCallback(this);
//        创建代理对象
        TrainStation proxyObject = (TrainStation) enhancer.create();
        return proxyObject;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        log.info("代售点收取一定的服务费！cglb动态代理");
        return method.invoke(trainStation,objects);
    }
}
